package cn.xdwanj.config

import com.baomidou.mybatisplus.annotation.DbType
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
import org.mybatis.spring.annotation.MapperScan
//import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
//import com.github.pagehelper.PageInterceptor
import org.mybatis.spring.mapper.MapperScannerConfigurer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import javax.sql.DataSource

@Configuration
@MapperScan("com.xdwanj.mapper")
open class MyBatisConfig {

  // 改为 MP 的 SqlSessionFactory
  @Bean
  open fun sqlSessionFactory(
    dataSource: DataSource
  ) = MybatisSqlSessionFactoryBean().apply {
    this.setDataSource(dataSource)
//    this.setConfigLocation(PathMatchingResourcePatternResolver().getResource("mybatis-config.xml"))
//    this.setConfigLocation(ClassPathResource("mybatis-config.xml"))
    // 设置别名扫包
    this.setTypeAliasesPackage("cn.xdwanj.entity")
    // 设置分页插件
    this.setPlugins(
//      arrayOf(PageInterceptor())
      MybatisPlusInterceptor().apply { addInnerInterceptor(PaginationInnerInterceptor(DbType.MYSQL)) }
    )
  }

  @Bean
  open fun mapperScannerConfigurer() = MapperScannerConfigurer().apply {
//    // 扫描接口所在的包
//    this.setBasePackage("com.xdwanj.mapper")
  }

  // 默认是开启的
//  @Bean
//  open fun configuration() = IbatisSessionConfiguration().apply {
//    this.isMapUnderscoreToCamelCase = true
//  }
}